package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.AttLeave;
import com.cssl.pojo.SalSalary;
import com.cssl.pojo.SysStaff;
import com.cssl.service.AttLeaveService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class AttleaveController {
    @Resource
    private AttLeaveService service;

    @GetMapping("/add")
    public boolean add(String type,int day,String date,String remark,int id){
        AttLeave attLeave=new AttLeave();
        attLeave.setDays(day);
        attLeave.setLeave_type(type);
        attLeave.setRemark(remark);
        attLeave.setStart_time(date);
        attLeave.setStaff_id(id);
        if (service.save(attLeave)){
            return true;
        }
        return false;
    }

    @GetMapping("/cx")
    public IPage<AttLeave> select(Integer pageIndex, Integer pageSize){
        if (pageIndex ==null||pageIndex == 0){
            pageIndex =1;
        }
        if (pageSize == null || pageSize ==0){
            pageSize =5;
        }
        IPage<AttLeave> page=service.select(new Page<AttLeave>(pageIndex, pageSize),new QueryWrapper<AttLeave>());
        return page;
    }

    @GetMapping("/tjcx")
    public IPage<AttLeave> select2(Integer pageIndex, Integer pageSize,Integer deptid,String sid){
        if (pageIndex ==null||pageIndex == 0){
            pageIndex =1;
        }
        if (pageSize == null || pageSize ==0){
            pageSize =5;
        }
        System.out.println(pageIndex+","+pageSize);
        if ((deptid == null || deptid == 0) && (sid != null && sid!="")) {
            IPage<AttLeave> page=service.select(new Page<AttLeave>(pageIndex, pageSize),
                    new QueryWrapper<AttLeave>().eq("s.code",sid));
            return page;
        }else if((sid == null || sid == "") && (deptid != null && deptid >0)){
            IPage<AttLeave> page=service.select(new Page<AttLeave>(pageIndex, pageSize), new QueryWrapper<AttLeave>().eq("s2.id",deptid));
            return page;
        } else if ((sid != null && sid!="")&& (deptid != null && deptid >0)) {
            IPage<AttLeave> page=service.select(new Page<AttLeave>(pageIndex, pageSize), new QueryWrapper<AttLeave>().eq("s2.id",deptid)
                    .eq("s.code",sid));
            return page;
        }

        return select(pageIndex, pageSize);
    }

    @GetMapping("/update")
    public boolean update(int status,int id,String cause){
        if (service.updateStatus(status,id,cause)>0){
            return true;
        }
        return false;
    }
}
